<?php

// 拍卖活动管理

namespace app\console\controller;

use think\Db;
use think\Url;
use think\Loader;
use think\Request;
use think\Controller;
use app\client\model\Express as ExpressModel;
use app\console\model\Auction as AuctionModel;
use app\console\model\AuctionOrder as AuctionOrderModel;

class Auction extends Controller
{

    /**
     * 拍卖活动列表
     * @param Request $request
     * @param AuctionModel $auction
     * @return mixed
     */
    public function index(Request $request, AuctionModel $auction)
    {
        // 获取参数
        $inquire = $request->param();

        // 筛选
        $condition['id'] = ['neq', '0'];
        if (!empty($inquire['title'])) $condition['title'] = ['like', '%' . $inquire['title'] . '%'];

        // 获取数据
        $data = $auction->where($condition)->order('create_time', 'desc')->paginate(10, false, ['query' => $inquire]);


        return $this->fetch('', [
            'mate_title' => '拍卖活动列表',
            'data'       => $data
        ]);
    }


    /**
     * 新增拍卖活动
     * @param Request $request
     * @param AuctionModel $auction
     * @return mixed
     */
    public function create(Request $request, AuctionModel $auction)
    {
        if ($request->isPost()) {
            // 获取参数
            $data = $request->param();

            // 验证
            $validate = Loader::validate('Auction');
            if (!$validate->check($data)) fail($validate->getError());

            // 新增
            $operation = $auction->allowField(true)->save($data);

            if ($operation) {
                win('操作成功', Url::build('Auction/index'));
            }

            fail('操作失败');
        }


        return $this->fetch('', [
            'mate_title' => '新增拍卖活动',
        ]);
    }


    public function update($id, Request $request, AuctionModel $auction)
    {
        if ($request->isPost()) {
            // 获取参数
            $data = $request->param();

            // 验证
            $validate = Loader::validate('Auction');
            if (!$validate->check($data)) fail($validate->getError());

            // 新增
            $operation = $auction->allowField(true)->save($data, ['id' => $id]);

            if ($operation) {
                win('操作成功', Url::build('Auction/index'));
            }

            fail('操作失败');
        }

        $item = $auction::get($id);


        return $this->fetch('create', [
            'mate_title' => '编辑拍卖活动',
            'item'       => $item
        ]);
    }


    public function order()
    {

        $data = Db::view('auction_order','*')->view('member','username,nickname','auction_order.member_id = member.id')->paginate(10,false);



        return $this->fetch('', [
            'mate_title' => '拍卖订单',
            'data'       => $data
        ]);
    }


    /*
     * 拍卖订单
     */
    public function order_examine()
    {
        $id = Request::instance()->param('id');

        if (Request::instance()->isPost()) {

            $data = Request::instance()->param();

            // 提交数据
            $dispose = (new AuctionOrderModel())->allowField(true)->save($data,['id'=>$data['id']]);

            if ($dispose) {
                win('操作成功', Url::build('Auction/order'));
            }

            fail('操作失败');

        }

        $item = Db::view('auction_order','*')->view('member','username,nickname','auction_order.member_id = member.id')->where('auction_order.id',$id)->find();

        $express_list = ExpressModel::all();


        return $this->fetch('', [
            'mate_title' => '拍卖订单管理',
            'item'       => $item,
            'express_list' => $express_list,
        ]);
    }
}